package org.jeecg.modules.scm.service.impl;

import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.scm.dto.ItemSearchDto;
import org.jeecg.modules.scm.entity.ScmCusInfo;
import org.jeecg.modules.scm.entity.ScmInvestor;
import org.jeecg.modules.scm.entity.ScmItem;
import org.jeecg.modules.scm.mapper.ScmItemMapper;
import org.jeecg.modules.scm.service.IScmCusInfoService;
import org.jeecg.modules.scm.service.IScmInvestorService;
import org.jeecg.modules.scm.service.IScmItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @Description: 项目信息表服务实现类
 * @Author: zzq
 * @Date: 2021-10-21
 * @Version: V1.0
 */
@Service
public class ScmItemServiceImpl extends ServiceImpl<ScmItemMapper, ScmItem> implements IScmItemService {

    @Autowired
    private IScmCusInfoService iScmCusInfoService;
    @Autowired
    private IScmInvestorService iScmInvestorService;

    @Override
    public Integer getCusId(Integer itemId) {
        return lambdaQuery().select(ScmItem::getCusNo)
                .eq(ScmItem::getItemId, itemId)
                .one().getCusNo();
    }

    @Override
    public boolean updateStatus(Integer itemId, Integer status) {
        return lambdaUpdate().eq(ScmItem::getItemId, itemId)
                .set(ScmItem::getStatus, status).update();
    }

    @Override
    public List<Map<String, Object>> likeName(String key) {
        LambdaQueryWrapper<ScmItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ScmItem::getItemName, ScmItem::getItemId)
                .like(ScmItem::getItemName, key);
        return baseMapper.selectMaps(queryWrapper);
    }

    @Override
    public Page<ScmItem> searchItemList(ItemSearchDto dto) {
        LambdaQueryWrapper<ScmItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getEsId() > 0, ScmItem::getCusNo, dto.getEsId())
                .eq(dto.getItemId() > 0, ScmItem::getItemId, dto.getItemId());
        // TODO 时间范围条件
        return baseMapper.selectPage(new Page<>(dto.getPageNo(), dto.getPageSize()), queryWrapper);
    }

    @Override
    public String uploadAccessory(MultipartFile file, Integer itemId) {
        ScmItem scmItem = lambdaQuery().select(ScmItem::getItemId, ScmItem::getAccessory).eq(ScmItem::getItemId, itemId).one();
        System.out.println(scmItem);
        if (scmItem == null) {
            throw new JeecgBootException("项目不存在");
        }
        String urls = scmItem.getAccessory();
        String url = CommonUtils.upload(file, "/item", CommonConstant.UPLOAD_TYPE_MINIO);
        String path = URLUtil.getPath(url);
        if (StringUtils.isEmpty(urls)) {
            urls = path;
        } else {
            urls = urls + "," + path;
        }
        lambdaUpdate().eq(ScmItem::getItemId, itemId)
                .set(StringUtils.isNotBlank(path), ScmItem::getAccessory, urls)
                .update();

        return urls;
    }


    @Override
    public List<String> getItemByCusName(String cusName) {
        QueryWrapper<ScmItem> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ScmItem::getCusName,cusName);
        List<ScmItem> scmItems = baseMapper.selectList(queryWrapper);
        List<String> list = new ArrayList<>();
        for(ScmItem scmItem:scmItems){
            list.add(scmItem.getItemName());
        }
        return list;
    }

    /**
     * 新增检查
     * */
    @Override
    public void check(ScmItem scmItem) {
        if (StringUtils.isBlank(scmItem.getCusName()) ||
                scmItem.getCusNo() == null ||
                scmItem.getInvestor() == null) {
            throw new JeecgBootException("请输入完整信息");
        }
        boolean cusExists = iScmCusInfoService.lambdaQuery()
                .eq(ScmCusInfo::getCusNo, scmItem.getCusNo())
                .eq(ScmCusInfo::getCusName, scmItem.getCusName()).exists();
        if (!cusExists) {
            throw new JeecgBootException("客户信息不存在");
        }
        boolean exists = iScmInvestorService.lambdaQuery()
                .eq(ScmInvestor::getBankNo, scmItem.getMainInvestor())
                .exists();
        if (!exists) {
            throw new JeecgBootException("资金方信息不存在");
        }

    }

    @Override
    public Map<String,Object> getItemNum(String cusName, Integer year) {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        List<Integer> res = new ArrayList<>();
        Map<String,Object> map = new HashedMap();
        int num = 0;
        String tmp;
        for(int i=1;i<=12;i++){
            if(i<=month){
                Integer itemNum = baseMapper.getItemNum(year, i,null, cusName);
                res.add(itemNum);
                num+=itemNum;
            }else{
                res.add(0);
            }
        }
        Integer itemNum = baseMapper.getItemNum(year, month, day, cusName);
        map.put("month",res);
        map.put("day",itemNum);
        map.put("year",num);
        return map;
    }
}
